package com.changgou.order.controller;

import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import com.changgou.util.Result;
import com.changgou.util.StatusCode;
import com.changgou.util.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author tang.qingbo
 * @Date 2020/9/5 0:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/cart")
//@CrossOrigin
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 根据选中的商品id,查询对应的商品信息
     */
    @GetMapping("/list/choose")
    public Result findChooseList(String[] ids) {
        //获取当前登录用户名
        String username = TokenDecode.getUserInfo().get("username");
        List<OrderItem> chooseList = cartService.findChooseList(ids, username);
        return new Result(true, StatusCode.OK, "查询选中商品列表成功", chooseList);
    }

    /**
     * 添加购物车数据
     */
    @RequestMapping("/add")
    public Result add(Integer num, String id) {
        //用户名
        //String username = "szitheima";
        String username = TokenDecode.getUserInfo().get("username");
        cartService.add(num, id, username);
        return new Result(true, StatusCode.OK, "加入购物车成功");
    }

    /**
     * 查询用户购物车数据
     */
    @GetMapping("/list")
    public Result list() {
        //用户名
        //String username = "szitheima";
        String username = TokenDecode.getUserInfo().get("username");
        List<OrderItem> orderItemList = cartService.list(username);
        return new Result(true, StatusCode.OK, "查询购物车列表成功",orderItemList);
    }
}
